import java.util.Arrays;

public class MyArrayList implements IList {
    public int[]array;
    public int useSize;
    public static final int DEFAULT_CAPAITY=10;
    public MyArrayList(){
        this.array=new int[DEFAULT_CAPAITY];
    }

    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
      this.array[this.useSize]=data;
      useSize++;
    }
    //判断有没有满
    public boolean isFull(){
        return this.array.length==useSize;
    }
    //扩容
    private void grow(){
        this.array= Arrays.copyOf(this.array,2*this.array.length);
    }
    //判断插入位置是否合理
    private void checkPos(int pos){
        if(pos<0||pos>this.useSize){
         throw new posIllegal("pos位置不合法");
        }
    }
    @Override
    public void add(int pos, int data) {
        try{
            checkPos(pos);
            if(isFull()){
                grow();
            }
            //挪动元素
            for(int i=useSize-1;i>=pos;i--){
                this.array[i+1]=array[i];
            }
            array[pos]=data;
            useSize++;
        }catch (posIllegal e){
            System.out.println("插入位置不合法....");
            e.printStackTrace();
        }

    }

    @Override
    public boolean contains(int toFind) {
        for(int i=0;i<useSize;i++){
            if(array[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i=0;i<useSize;i++){
            if(array[i]==toFind){
                return i;
            }
        }
        return 0;
    }
    private void checkPos2(int pos) {
        if (pos < 0 || pos >= this.useSize) {
            throw new posIllegal("pos位置不合法");
        }
    }
    @Override
    public int get(int pos) {

        try{
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch (posIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
        return -1;
    }
    private void checkEmpty(){
        if(isEmpty()){
            throw new EmptyException("顺序表为空");
        }
    }
    public boolean isEmpty(){
        return useSize==0;
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkEmpty();
            checkPos2(pos);
            array[pos]=value;
        }catch (posIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try{
            checkEmpty();
            int pos=indexOf(toRemove);
            if(pos==-1){
                return;
            }
            for(int i=pos;i<useSize-1;i++){
                array[i]=array[i+1];
            }
            useSize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
       return this.useSize;
    }

    @Override
    public void clear() {
       useSize=0;
    }

    @Override
    public void display() {
     for (int i=0;i<this.useSize;i++){
         System.out.println(array[i]+" ");
     }
    }
}
